﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.WowAddonStudio.Lua.Parser;

namespace Microsoft.WowAddonStudio.Lua.AST
{
    /// <summary>
    /// Represents a table constructor node in the AST for the Lua code file.
    /// </summary>
    public class TableConstructor : Node
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TableConstructor"/> class.
        /// </summary>
        /// <param name="location">The location of the node.</param>
        public TableConstructor(LexLocation location)
            : base(location)
        {
        }

		/// <summary>
		/// Gets the linked list of fields in the table constructor.
		/// </summary>
		/// <value>The field list.</value>
        public Field FieldList { get; set; }

        /// <summary>
        /// Gets the fields.
        /// </summary>
        /// <returns>An enumerable collection of fields.</returns>
        public IEnumerable<Field> GetFields()
        {
            return GetChildNodes().Cast<Field>();
        }

        /// <summary>
		/// Gets the child nodes.
		/// </summary>
		/// <returns>An enumerable collection of nodes.</returns>
        public override IEnumerable<Node> GetChildNodes()
        {
            if (FieldList != null)
            {
                foreach (Field field in FieldList)
                    yield return field;
            }
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return "TableConstructor";
        }
    }
}
